package com.hospital.register.core.controller.api;

import com.hospital.register.common.result.Result;
import com.hospital.register.core.service.DepartmentService;
import com.hospital.register.core.service.HospitalSetService;
import com.hospital.register.dto.core.api.DepartmentPageDto;
import com.hospital.register.dto.core.api.DepartmentRemoveDto;
import com.hospital.register.dto.core.api.DepartmentSaveDto;
import com.hospital.register.model.core.Department;
import com.hospital.register.vo.core.api.DepartmentPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "科室 api",description = "面向医院")
@RestController
@RequestMapping("/api/hosp")
@Validated
//@CrossOrigin
public class DepartmentApiController {

    /**
     * 处理科室相关业务
     */
    @Autowired
    private DepartmentService departmentService;

    /**
     * 处理医院设置相关业务
     */
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "上传科室")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(@Validated DepartmentSaveDto dto,
                                 @ApiIgnore HttpServletRequest request) {
        this.hospitalSetService.checkSignKey(dto.getHoscode(), dto.getSign(), request);//验签

        Department department = new Department();
        BeanUtils.copyProperties(dto, department);

        if (this.departmentService.exists(dto.getHoscode(), dto.getDepcode())) {//科室存在
            this.departmentService.update(department);
        } else {
            this.departmentService.insert(department);
        }
        return Result.ok();
    }

    @ApiOperation(value = "分页查询科室")
    @PostMapping("/department/list")
    public Result<Page<DepartmentPageVo>> listDepartment(@Validated DepartmentPageDto dto,
                                                         @ApiIgnore HttpServletRequest request) {
        this.hospitalSetService.checkSignKey(dto.getHoscode(), dto.getSign(), request);
        Page<DepartmentPageVo> page = this.departmentService.findPage(dto);
        return Result.ok(page);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("/department/remove")
    public Result removeDepartment(@Validated DepartmentRemoveDto dto,
                                   @ApiIgnore HttpServletRequest request) {
        this.hospitalSetService.checkSignKey(dto.getHoscode(), dto.getSign(), request);
        this.departmentService.remove(dto.getHoscode(), dto.getDepcode());
        return Result.ok();
    }

}
